package cn.eyesea.cc.frame.factory;

import cn.eyesea.cc.frame.base.Demand;
import cn.eyesea.cc.frame.base.ResultResource;
import cn.eyesea.cc.util.FileUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 生产资源库
 *
 * @author wanghuilong
 * @version 1.0
 * @date 2021/7/26 1:31 下午
 */
public class ProductionResourceFactory {

    static List<ResultResource> resultResources = new ArrayList<>();

    public static void registerProductionResource(ResultResource resultResource) {
        if (!resultResources.contains(resultResource)) {
            resultResources.add(resultResource);
            printSourceCode(Arrays.asList(resultResource));
        }
    }


    public static ResultResource search(Demand demand) {
        //TODO
        return null;
    }


    public static void printSourceCode(List<ResultResource> resultResources) {
        for (ResultResource resultResource : resultResources) {
            if (null != resultResource && StringUtils.isNotBlank(resultResource.getGenerateCode())
                    && resultResource.getResourceType().getIsFile()) {
                FileUtil.writeToFile(resultResource.getGenerateCode(), resultResource.getResourcePath());
            }
        }
    }
}
